<!DOCTYPE html>
<html>
<!-- Created by GNU Texinfo 7.1.1, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- This file documents the use of the GNU compilers.

Copyright © 1988-2023 Free Software Foundation, Inc.

Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being "Funding Free Software", the Front-Cover
Texts being (a) (see below), and with the Back-Cover Texts being (b)
(see below).  A copy of the license is included in the section entitled
"GNU Free Documentation License".

(a) The FSF's Front-Cover Text is:

A GNU Manual

(b) The FSF's Back-Cover Text is:

You have freedom to copy and modify this GNU Manual, like GNU
     software.  Copies published by the Free Software Foundation raise
     funds for GNU development. -->
<title>Type encoding (Using the GNU Compiler Collection (GCC))</title>

<meta name="description" content="Type encoding (Using the GNU Compiler Collection (GCC))">
<meta name="keywords" content="Type encoding (Using the GNU Compiler Collection (GCC))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta name="viewport" content="width=device-width,initial-scale=1">

<link href="index.html" rel="start" title="Top">
<link href="Indices.html" rel="index" title="Indices">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Objective_002dC.html" rel="up" title="Objective-C">
<link href="Garbage-Collection.html" rel="next" title="Garbage Collection">
<link href="Executing-code-before-main.html" rel="prev" title="Executing code before main">
<style type="text/css">
<!--
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
div.example {margin-left: 3.2em}
kbd.key {font-style: normal}
span:hover a.copiable-link {visibility: visible}
-->
</style>


</head>

<body lang="en_US">
<div class="section-level-extent" id="Type-encoding">
<div class="nav-panel">
<p>
Next: <a href="Garbage-Collection.html" accesskey="n" rel="next">Garbage Collection</a>, Previous: <a href="Executing-code-before-main.html" accesskey="p" rel="prev"><code class="code">+load</code>: Executing Code before <code class="code">main</code></a>, Up: <a href="Objective_002dC.html" accesskey="u" rel="up">GNU Objective-C Features</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Indices.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<h3 class="section" id="Type-Encoding"><span>8.3 Type Encoding<a class="copiable-link" href="#Type-Encoding"> &para;</a></span></h3>

<p>This is an advanced section.  Type encodings are used extensively by
the compiler and by the runtime, but you generally do not need to know
about them to use Objective-C.
</p>
<p>The Objective-C compiler generates type encodings for all the types.
These type encodings are used at runtime to find out information about
selectors and methods and about objects and classes.
</p>
<p>The types are encoded in the following way:
</p>

<table class="multitable">
<tbody><tr><td width="25%"><code class="code">_Bool</code></td><td width="75%"><code class="code">B</code></td></tr>
<tr><td width="25%"><code class="code">char</code></td><td width="75%"><code class="code">c</code></td></tr>
<tr><td width="25%"><code class="code">unsigned char</code></td><td width="75%"><code class="code">C</code></td></tr>
<tr><td width="25%"><code class="code">short</code></td><td width="75%"><code class="code">s</code></td></tr>
<tr><td width="25%"><code class="code">unsigned short</code></td><td width="75%"><code class="code">S</code></td></tr>
<tr><td width="25%"><code class="code">int</code></td><td width="75%"><code class="code">i</code></td></tr>
<tr><td width="25%"><code class="code">unsigned int</code></td><td width="75%"><code class="code">I</code></td></tr>
<tr><td width="25%"><code class="code">long</code></td><td width="75%"><code class="code">l</code></td></tr>
<tr><td width="25%"><code class="code">unsigned long</code></td><td width="75%"><code class="code">L</code></td></tr>
<tr><td width="25%"><code class="code">long long</code></td><td width="75%"><code class="code">q</code></td></tr>
<tr><td width="25%"><code class="code">unsigned long long</code></td><td width="75%"><code class="code">Q</code></td></tr>
<tr><td width="25%"><code class="code">float</code></td><td width="75%"><code class="code">f</code></td></tr>
<tr><td width="25%"><code class="code">double</code></td><td width="75%"><code class="code">d</code></td></tr>
<tr><td width="25%"><code class="code">long double</code></td><td width="75%"><code class="code">D</code></td></tr>
<tr><td width="25%"><code class="code">void</code></td><td width="75%"><code class="code">v</code></td></tr>
<tr><td width="25%"><code class="code">id</code></td><td width="75%"><code class="code">@</code></td></tr>
<tr><td width="25%"><code class="code">Class</code></td><td width="75%"><code class="code">#</code></td></tr>
<tr><td width="25%"><code class="code">SEL</code></td><td width="75%"><code class="code">:</code></td></tr>
<tr><td width="25%"><code class="code">char*</code></td><td width="75%"><code class="code">*</code></td></tr>
<tr><td width="25%"><code class="code">enum</code></td><td width="75%">an <code class="code">enum</code> is encoded exactly as the integer type that the compiler uses for it, which depends on the enumeration
values.  Often the compiler users <code class="code">unsigned int</code>, which is then encoded as <code class="code">I</code>.</td></tr>
<tr><td width="25%">unknown type</td><td width="75%"><code class="code">?</code></td></tr>
<tr><td width="25%">Complex types</td><td width="75%"><code class="code">j</code> followed by the inner type.  For example <code class="code">_Complex double</code> is encoded as &quot;jd&quot;.</td></tr>
<tr><td width="25%">bit-fields</td><td width="75%"><code class="code">b</code> followed by the starting position of the bit-field, the type of the bit-field and the size of the bit-field (the bit-fields encoding was changed from the NeXT&rsquo;s compiler encoding, see below)</td></tr>
</tbody>
</table>


<p>The encoding of bit-fields has changed to allow bit-fields to be
properly handled by the runtime functions that compute sizes and
alignments of types that contain bit-fields.  The previous encoding
contained only the size of the bit-field.  Using only this information
it is not possible to reliably compute the size occupied by the
bit-field.  This is very important in the presence of the Boehm&rsquo;s
garbage collector because the objects are allocated using the typed
memory facility available in this collector.  The typed memory
allocation requires information about where the pointers are located
inside the object.
</p>
<p>The position in the bit-field is the position, counting in bits, of the
bit closest to the beginning of the structure.
</p>
<p>The non-atomic types are encoded as follows:
</p>

<table class="multitable">
<tbody><tr><td width="20%">pointers</td><td width="80%">&lsquo;<samp class="samp">^</samp>&rsquo; followed by the pointed type.</td></tr>
<tr><td width="20%">arrays</td><td width="80%">&lsquo;<samp class="samp">[</samp>&rsquo; followed by the number of elements in the array followed by the type of the elements followed by &lsquo;<samp class="samp">]</samp>&rsquo;</td></tr>
<tr><td width="20%">structures</td><td width="80%">&lsquo;<samp class="samp">{</samp>&rsquo; followed by the name of the structure (or &lsquo;<samp class="samp">?</samp>&rsquo; if the structure is unnamed), the &lsquo;<samp class="samp">=</samp>&rsquo; sign, the type of the members and by &lsquo;<samp class="samp">}</samp>&rsquo;</td></tr>
<tr><td width="20%">unions</td><td width="80%">&lsquo;<samp class="samp">(</samp>&rsquo; followed by the name of the structure (or &lsquo;<samp class="samp">?</samp>&rsquo; if the union is unnamed), the &lsquo;<samp class="samp">=</samp>&rsquo; sign, the type of the members followed by &lsquo;<samp class="samp">)</samp>&rsquo;</td></tr>
<tr><td width="20%">vectors</td><td width="80%">&lsquo;<samp class="samp">![</samp>&rsquo; followed by the vector_size (the number of bytes composing the vector) followed by a comma, followed by the alignment (in bytes) of the vector, followed by the type of the elements followed by &lsquo;<samp class="samp">]</samp>&rsquo;</td></tr>
</tbody>
</table>

<p>Here are some types and their encodings, as they are generated by the
compiler on an i386 machine:
</p>
<br>

<table class="multitable">
<thead><tr><th width="60%">Objective-C type</th><th width="40%">Compiler encoding</th></tr></thead>
<tbody><tr><td width="60%"><div class="example smallexample">
<pre class="example-preformatted">int a[10];
</pre></div></td><td width="40%"><code class="code">[10i]</code></td></tr>
<tr><td width="60%"><div class="example smallexample">
<pre class="example-preformatted">struct {
  int i;
  float f[3];
  int a:3;
  int b:2;
  char c;
}
</pre></div></td><td width="40%"><code class="code">{?=i[3f]b128i3b131i2c}</code></td></tr>
<tr><td width="60%"><div class="example smallexample">
<pre class="example-preformatted">int a __attribute__ ((vector_size (16)));
</pre></div></td><td width="40%"><code class="code">![16,16i]</code> (alignment depends on the machine)</td></tr>
</tbody>
</table>

<br>

<p>In addition to the types the compiler also encodes the type
specifiers.  The table below describes the encoding of the current
Objective-C type specifiers:
</p>
<br>

<table class="multitable">
<thead><tr><th width="25%">Specifier</th><th width="75%">Encoding</th></tr></thead>
<tbody><tr><td width="25%"><code class="code">const</code></td><td width="75%"><code class="code">r</code></td></tr>
<tr><td width="25%"><code class="code">in</code></td><td width="75%"><code class="code">n</code></td></tr>
<tr><td width="25%"><code class="code">inout</code></td><td width="75%"><code class="code">N</code></td></tr>
<tr><td width="25%"><code class="code">out</code></td><td width="75%"><code class="code">o</code></td></tr>
<tr><td width="25%"><code class="code">bycopy</code></td><td width="75%"><code class="code">O</code></td></tr>
<tr><td width="25%"><code class="code">byref</code></td><td width="75%"><code class="code">R</code></td></tr>
<tr><td width="25%"><code class="code">oneway</code></td><td width="75%"><code class="code">V</code></td></tr>
</tbody>
</table>

<br>

<p>The type specifiers are encoded just before the type.  Unlike types
however, the type specifiers are only encoded when they appear in method
argument types.
</p>
<p>Note how <code class="code">const</code> interacts with pointers:
</p>
<br>

<table class="multitable">
<thead><tr><th width="25%">Objective-C type</th><th width="75%">Compiler encoding</th></tr></thead>
<tbody><tr><td width="25%"><div class="example smallexample">
<pre class="example-preformatted">const int
</pre></div></td><td width="75%"><code class="code">ri</code></td></tr>
<tr><td width="25%"><div class="example smallexample">
<pre class="example-preformatted">const int*
</pre></div></td><td width="75%"><code class="code">^ri</code></td></tr>
<tr><td width="25%"><div class="example smallexample">
<pre class="example-preformatted">int *const
</pre></div></td><td width="75%"><code class="code">r^i</code></td></tr>
</tbody>
</table>

<br>

<p><code class="code">const int*</code> is a pointer to a <code class="code">const int</code>, and so is
encoded as <code class="code">^ri</code>.  <code class="code">int* const</code>, instead, is a <code class="code">const</code>
pointer to an <code class="code">int</code>, and so is encoded as <code class="code">r^i</code>.
</p>
<p>Finally, there is a complication when encoding <code class="code">const char *</code>
versus <code class="code">char * const</code>.  Because <code class="code">char *</code> is encoded as
<code class="code">*</code> and not as <code class="code">^c</code>, there is no way to express the fact
that <code class="code">r</code> applies to the pointer or to the pointee.
</p>
<p>Hence, it is assumed as a convention that <code class="code">r*</code> means <code class="code">const
char *</code> (since it is what is most often meant), and there is no way to
encode <code class="code">char *const</code>.  <code class="code">char *const</code> would simply be encoded
as <code class="code">*</code>, and the <code class="code">const</code> is lost.
</p>

<ul class="mini-toc">
<li><a href="Legacy-type-encoding.html" accesskey="1">Legacy Type Encoding</a></li>
<li><a href="_0040encode.html" accesskey="2"><code class="code">@encode</code></a></li>
<li><a href="Method-signatures.html" accesskey="3">Method Signatures</a></li>
</ul>
</div>
<hr>
<div class="nav-panel">
<p>
Next: <a href="Garbage-Collection.html">Garbage Collection</a>, Previous: <a href="Executing-code-before-main.html"><code class="code">+load</code>: Executing Code before <code class="code">main</code></a>, Up: <a href="Objective_002dC.html">GNU Objective-C Features</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Indices.html" title="Index" rel="index">Index</a>]</p>
</div>



</body>
</html>
